﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MySQL.Db;

namespace Gollie.DataTables
{
	public class MyExpiredCreditsItem
	{
		public string Bedrijfsnaam { get; set; }
		public string Straat { get; set; }
		public string Plaats { get; set; }
		public string Website { get; set; }
		public DateTime PurchaseDate { get; set; }
		public DateTime ExpirationDate { get; set; }
		public decimal Saldo { get; set; }
		public decimal Credit { get; set; }
		public decimal Cost { get; set; }
		public decimal Profit { get; set; }
		public uint VendorId { get; set; }
		public decimal Spent { get; set; }
	}

	public class MyExpiredCredits : BaseBindable<MyExpiredCreditsItem>
	{
		internal SortedDictionary<string, uint> vendors = new SortedDictionary<string, uint>() { { "<Alles>", 0 } };

		public MyExpiredCredits(uint userId)
		{
			ResultSet resultSet = Database.Queries.GetExpiredUserSaldi(userId);

			this.AddDetails(resultSet);
		}
		private void AddDetails(ResultSet resultSet)
		{
			int numRows = resultSet.Count;

			for (int i = 0; i < numRows; i++)
			{
				string vendor = resultSet.GetString(i, "name");
				uint id = resultSet.GetUInt32(i, "vendorid");
				decimal cost = resultSet.GetDecimal(i, "cost");
				decimal credit = resultSet.GetDecimal(i, "totcredit");
				decimal saldo = resultSet.GetDecimal(i, "saldo");
				decimal spent = credit - saldo;
				decimal profit = spent - cost;

				this.Add(new MyExpiredCreditsItem()
				{
					Bedrijfsnaam = vendor,
					Straat = resultSet.GetString(i, "street"),
					Plaats = resultSet.GetString(i, "city"),
					Website = resultSet.GetString(i, "website"),
					PurchaseDate = resultSet.GetDateTime(i, "purchasedate"),
					ExpirationDate = resultSet.GetDateTime(i, "expirationdate"),
					Saldo = saldo,
					Credit = credit,
					Cost = cost,
					Profit = profit,
					VendorId = id,
					Spent = spent
				});

				if (vendors.ContainsKey(vendor) == false)
					vendors[vendor] = id;
			}
		}
	}
}